package session;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "ServletSession" , urlPatterns = "/ss")
public class ServletSession extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // session 测试
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");


        HttpSession session = request.getSession();
        session.setAttribute("name","wf");
        // 在别的servlet 里面使用getAttribute("name") 取出
        // 里面使用removeAttribute("name") 去除
        // 里面使用removeAttribute("name") 去除
        // session.invalidate(); 删除sessionId(销毁session)  使session 失效  或者在<session-config>分钟为单位</session-config>里面配置

        //服务正常 浏览器打开就有(整个浏览器只要不关sessionId就不会变) 一个浏览器对应一个sessionId  这个东西可以辩出是不是那个客户
        String SESSIONID = session.getId();

        if (session.isNew()){
            response.getWriter().write("session创建成功,ID："+SESSIONID);
        }else {
            response.getWriter().write("session已经存在,ID："+SESSIONID);
        }
        // session创建的时候 有个sessionId  存给了cookie 即
        // Cookie c = new Cookie("JSESSIONID",SESSIONID)
        // response.addCookie(cookie)

    }
}
